﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using CdcSoftware.Pivotal.Applications.Core.Client;
using CdcSoftware.Pivotal.Engine.UI.Forms;
using CdcSoftware.Pivotal.Applications.Core.Common;
using CdcSoftware.Pivotal.Engine.Client.Services.Interfaces;
using System.Data;

namespace Pcm.Retail.Client.FormTasks
{
    public partial class RE_AddInstallBaseItem : FormClientTask
    {
        /// <summary>
        /// 
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="args"></param>
        /// <history>
        /// 
        /// 
        /// </history>
        public virtual void OnDataSourceUpdatedSerialNo(object sender, EventArgs args)
        {
            try
            {
                if (!Convert.IsDBNull(this.PrimaryDataRow["RE_RAE_Serial_Number"]))
                {
                    DataRow[] appleCareProducts = OrderProducts.AsEnumerable().Where(a => TypeConvert.ToString(a["RE_RAE_Serial_Number"]) ==
                                                                                            TypeConvert.ToString(this.PrimaryDataRow["RE_RAE_Serial_Number"])).ToArray();
                    //Check if Apple Care Product exists in this Order record.
                    if (appleCareProducts != null && appleCareProducts.Length > 0)
                    {
                        PivotalMessageBox.Show("Product is already add to the Order.");
                        return;
                    }

                    AppleCare.resellerWarrantyStatusResponseType warrantyResponse = this.CallAppleCareResellerWarrantyStatus();
                    if (warrantyResponse != null)
                    {
                        string str = warrantyResponse.warrantyDetailInfo.coverageEndDate;
                         DateTime? contractCoverageEndDate = null;
                        if (str != null && str != string.Empty)
                            contractCoverageEndDate = new DateTime(TypeConvert.ToInt32("20" + str.Substring(6, 2)),
                                                                        TypeConvert.ToInt32(str.Substring(0, 2)), TypeConvert.ToInt32(str.Substring(3, 2)));

                        DateTime? contractCoverageStartDate = null;
                        str = warrantyResponse.warrantyDetailInfo.contractCoverageStartDate;
                        if (str != null && str != string.Empty)
                            contractCoverageStartDate = new DateTime(TypeConvert.ToInt32("20" + str.Substring(6, 2)),
                                                                        TypeConvert.ToInt32(str.Substring(0, 2)), TypeConvert.ToInt32(str.Substring(3, 2)));

                        string serialNo = TypeConvert.ToString(PrimaryDataRow["RE_RAE_Serial_Number"]);
                        DateTime registrationDate = DateTime.Today;
                        // Call web service using primaryDataRow["RE_RAE_Serial_Number"].
                        if (contractCoverageEndDate > DateTime.Today && contractCoverageStartDate == null)
                        {
                            this.PrimaryDataRow["Product_Description"] = warrantyResponse.warrantyDetailInfo.productDescription;
                            this.PrimaryDataRow["Configuration_Description"] = warrantyResponse.warrantyDetailInfo.configDescription;                           
                        }
                        else
                        {
                            PivotalMessageBox.Show("The Serial Number " + serialNo + " is not eligible for Apple Care.");
                            this.PrimaryDataRow["RE_RAE_Serial_Number"] = DBNull.Value;
                            this.FormControl.GetControlByName("RERAESerialNumber").Focus();
                        }
                    }
                    else
                    {
                        this.PrimaryDataRow["RE_RAE_Serial_Number"] = DBNull.Value;
                        this.FormControl.GetControlByName("RERAESerialNumber").Focus();
                    }
                }
                //Call EnableDisableAddItemButton method to Enable/Disble AddItem Button.
                this.EnableDisableAddItemButton();
            }
            catch (Exception exc)
            {
                this.PrimaryDataRow["RE_RAE_Serial_Number"] = DBNull.Value;
                this.PrimaryDataRow["Product_Description"] = DBNull.Value;
                this.PrimaryDataRow["Configuration_Description"] =DBNull.Value;
                this.PrimaryDataRow["Product_Id"] = DBNull.Value;
                
                Globals.HandleException(exc, true);
            }
        }
        
        /// <summary>
        /// Event gets triggered with the click of Add Item Button.
        /// </summary>
        /// <param name="sender">The control associated with the event triggered</param>
        /// <param name="args">The argument for the event</param>
        /// <history>
        /// #Revision   Date            Author      Description
        /// 6.0.12.0     2013-06-04      Pkini       Initial version for Retail.
        /// </history>
        public virtual void OnButtonClickAddItem(PivotalControl sender, EventArgs args)
        {
            try
            {
                string message = string.Empty;
                this.FormView.ValidateRequiredFields(out message);

                if (message != string.Empty)
                {
                    PivotalMessageBox.Show(message, System.Windows.Forms.MessageBoxButtons.OK, System.Windows.Forms.MessageBoxIcon.Information);
                    return;
                }

                //Setting Global variable AddInstallBase.
                //This variable will be accessed in Sales client task to fetch the  Product_Id and Serial_Number.
                Globals.AddInstallBase = new object[] { this.PrimaryDataRow["Product_Id"], this.PrimaryDataRow["RE_RAE_Serial_Number"] };
                                    
                this.FormView.CloseForm(true);
            }
            catch (Exception exc)
            {
                Globals.HandleException(exc, true);
            }
        }

        /// <summary>
        /// Event gets triggered with the click of Cancel Button.
        /// </summary>
        /// <param name="sender">The control associated with the event triggered</param>
        /// <param name="args">The argument for the event</param>
        /// <history>
        /// #Revision   Date            Author      Description
        /// 6.0.12.0     2013-06-04      Pkini       Initial version for Retail.
        /// </history>
        public virtual void OnButtonClickCancel(PivotalControl sender, EventArgs args)
        {
            try 
            {
                this.FormView.CloseForm(true);
            }
            catch (Exception exc)
            {
                Globals.HandleException(exc, true);
            }
        }


        /// <summary>
        /// Event gets triggered with the OnDataSourceUpdate of Product ID Field.
        /// </summary>
        /// <param name="sender">The control associated with the event triggered</param>
        /// <param name="args">The argument for the event</param>
        /// <history>
        /// #Revision   Date            Author      Description
        /// 6.0.12.0     2013-06-04      Pkini       Initial version for Retail.
        /// </history>
        public virtual void OnDataSourceUpdateProductId(object sender, EventArgs args)
        {
            try
            {
                //Call EnableDisableAddItemButton method to Enable/Disble AddItem Button.
                this.EnableDisableAddItemButton();
            }
            catch (Exception exc)
            {
                Globals.HandleException(exc, true);
            }
        }
        
    }
}
